package number_43;

public class Solution {
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int n1 = num1.length(), n2 = num2.length();
        int[] num = new int[n1 + n2];
        for (int i = n1 - 1; i >= 0; i--) {
            int v1 = num1.charAt(i) - '0';
            for (int j = n2 - 1; j >= 0; j--) {
                int v2 = num2.charAt(j) - '0';
                num[i + j + 1] += v1 * v2;
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = num.length - 1; i >= 1; i--) {
            num[i - 1] += num[i] / 10;
            num[i] %= 10;
            sb.append(num[i]);
        }

        if (num[0] != 0) {
            sb.append(num[0]);
        }
        sb.reverse();
        return sb.toString();
    }

    public static void main(String[] args) {
        new Solution().multiply("123", "456");
    }
}
